layui.use(['form', 'jquery', 'layer', 'table', 'laydate'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var department_id = '';
	if(department_form_value.department_id != undefined) {
		department_id = department_form_value.department_id;
		department_form_value.master_department_goods = department_form_value.master_department_goods == 1 ? true : false;
		if(department_form_value.goods_alone == 1) {
			$('.goods-audit-div').hide();
			department_form_value.goods_alone = true;
			department_form_value.goods_audit = false;
		} else {
			$('.goods-audit-div').show();
			department_form_value.goods_alone = false;
			department_form_value.goods_audit = department_form_value.goods_audit == 1 ? true : false;
		}
	}
	var lay_filter = 'tpl_department' + department_id;
	form.val(lay_filter, department_form_value);
	// 表单渲染
	form.render('checkbox');

	// 监听复选框
	form.on('checkbox(department_goods_alone)', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if(value == true) {
			$('.goods-audit-div').hide();
		} else {
			$('.goods-audit-div').show();
		}
	})
	// 监听提交
	form.on('submit(submit_department)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if(field.department_name == '') {
			layer.msg('部门名称必须填写', {
				icon: 2,
				amim: 6
			})
			$('input[name="department_name"]').addClass('.layui-form-danger').focus();
			return false;
		}
		send_info(url, field, function(data) {
			if(data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="department"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="department_list"]').trigger('click');
				// 表格重载
				if($('table[lay-filter="department_list"]').length > 0){
					if(field.department_id == undefined || field.department_id == '') {
						table.reload('department_list');
					} else {
						table.editTr('department_list', 'department_id', field.department_id);
					}
				}
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})

})